<template>
  <div class="main-carousel">
    <div class="container">
      <transition-group name="fade" tag="div" class="fade">
        <div class="fade-item" v-for="(item,index) in fadeItems" v-show="index === curIndex" :key="''+index">
          <router-link :to="'/GoodsList/'+item.id">
            <img :src="item.imgUrl" alt="">
          </router-link>
        </div>
      </transition-group>
      <div class="play-pre" @click="playPre">
        <span class="icon"></span>
      </div>
      <div class="play-next" @click="playNext">
        <span class="icon"></span>
      </div>
      <div class="play-dot">
        <span class="dot" v-for="n in 5" :class="{active: n === curIndex + 1}" :key="n"></span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      curIndex: 0,
      timeId: '',
      fadeItems: [
        {imgUrl: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/68f87aa0faf72e04034f9354ac75b9a8.jpg?w=2452&h=920', id: 1},
        {imgUrl: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/ebfbc53555aa0652765778879153a82e.jpg?thumb=1&w=1226&h=460&f=webp&q=90', id: 2},
        {imgUrl: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/369f3e3fc4efa7f27c0433995e6434bd.jpg?thumb=1&w=1226&h=460&f=webp&q=90', id: 3},
        {imgUrl: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/9ebff5f5c1f52f2dbdd611897adbefd4.jpg?thumb=1&w=1226&h=460&f=webp&q=90', id: 4}
      ]
    }
  },
  mounted () { // Vue2.0 替换了之前的ready，详见文档生命周期函数mounted
    this.autoPlay()
  },
  methods: {
    playNext () {
      let lastIndex = this.fadeItems.length - 1
      if (this.curIndex < lastIndex) {
        this.curIndex += 1
      } else {
        this.curIndex = 0
      }
    },
    playPre () {
      let lastIndex = this.fadeItems.length - 1
      if (this.curIndex > 0) {
        this.curIndex -= 1
      } else {
        this.curIndex = lastIndex
      }
    },
    autoPlay () {
      clearInterval(this.timeId)
      this.timeId = setInterval(() => {
        this.playNext()
      }, 4000)
    }
  }
}
</script>

<style scoped lang="scss">
  .main-carousel{
    position: absolute;
    left: 0;
    top: 0;
    width: 1226px;
    background: #fff;
    height: 460px;
    z-index: 1;
    .container{
      position: relative;
      height: 460px;
      .fade-item{
        position: absolute;
        left: 0;
        top: 0;
        width: 1226px;
        z-index: 1;
      }
      .play-pre{
        position: absolute;
        left: 234px;
        top: 50%;
        margin-top: -35px;
        z-index: 2;
        .icon{
          width: 41px;
          height: 69px;
          background: url(../../assets/icon-slides.png) no-repeat;
          background-position: -83px 50%;
          display: inline-block;
          cursor: pointer;
          &:hover{
            background: url(../../assets/icon-slides.png) no-repeat;
            background-position: 1px 50%;
          }
        }
      }
      .play-next{
        position: absolute;
        right: 0;
        top: 50%;
        margin-top: -35px;
        z-index: 2;
        .icon{
          width: 41px;
          height: 69px;
          background: url(../../assets/icon-slides.png) no-repeat;
          background-position: -124px 50%;
          display: inline-block;
          cursor: pointer;
          &:hover{
            background: url(../../assets/icon-slides.png) no-repeat;
            background-position: -42px 50%;
          }
        }
      }
      .play-dot{
        position: absolute;
        z-index: 1;
        width: 100px;
        right: 20px;
        bottom: 20px;
        display: flex;
        justify-content: space-between;
        .dot{
          width: 6px;
          height: 6px;
          border: 2px solid rgba(255,255,255,0.5);
          border-radius: 50%;
          background: rgba(0,0,0,0.5);
        }
        .active{
          border-color: rgba(0,0,0,0.5);
          background: rgba(255,255,255,0.5);
        }
      }
    }
  }
.fade-enter-active, .fade-leave-active {
  transition: opacity .5s ease;
}
.fade-enter, .fade-leave-to /* .fade-leave-active in <2.1.8 */ {
  opacity: 0;
}
</style>
